package cn.edu.ujn.zzylab2.service;

import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.zzylab2.dao.Orders;
import cn.edu.ujn.zzylab2.dao.OrdersMapper;
import cn.edu.ujn.zzylab2.dao.Product;
@Service("ordersService")
@Transactional
public class OrdersServiceImpl implements IOrdersService {
@Autowired
	private OrdersMapper ordersMapper;
	
	@Override
	public int insert(Orders record) {
        int insert = this.ordersMapper.insert(record);
		
    	if(insert>0) {
			List<Product> productList = record.getProductList();
			for(Product p:productList) {
				HashMap<String, Integer> orderItem = new HashMap<String, Integer>();
				orderItem.put("orders_id", record.getId());
				orderItem.put("product_id", p.getId());
				int addOrdersItem = ordersMapper.addOrdersItem(orderItem);
				System.out.println("插入成功："+addOrdersItem);
			}
		}
		return insert;
	}
	
	

	@Override
	public List<Orders> findAllOrders() {
		List<Orders> allOrders = this.ordersMapper.findAllOrders();
		return allOrders;
	}

	@Override
	public Orders findById(int id) {
		Orders order = this.ordersMapper.selectByPrimaryKey(id);
		return order;
	}





}
